package com.znxs.bs_c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.znxs.bs_c.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.znxs.bs_c.service.IMenuService;
import com.znxs.bs_c.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 左拿
 * @since 2023-08-21
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private IMenuService menuService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Menu menu) {
        return menuService.saveOrUpdate(menu) ? Result.success() : Result.error(500, "保持失败");
    }

    // 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return menuService.removeById(id) ? Result.success() : Result.error(500, "删除失败");
    }

    // 根据id批量删除
    @DeleteMapping
    public Result deleteByIds(@RequestBody List<Integer> Ids) {
        return menuService.removeByIds(Ids) ? Result.success() : Result.error(500, "删除失败");
    }

    // 查询所有
    @GetMapping
    public Result findAll( @RequestParam(value = "name",required = false) String name) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.like("name", name);
        // 查询所有数据
        List<Menu> menuList = menuService.list(menuQueryWrapper);
        // 根据menuList 查询对应的树形结构
        List<Menu> treeMenuList = menuService.getTreeMenuList(menuList);
        return Result.success(treeMenuList);
    }

    // 查询所有的icons图标
//    @GetMapping("/icon")
//    public Result getIcons(){
//        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
//        menuQueryWrapper.select("icon");
//        List<Menu> iconList = menuService.list(menuQueryWrapper);
//        return Result.success(iconList);
//    }

    // 根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        return Result.success(menuService.getById(id));
    }

//    // 分页查询
//    @GetMapping("/page")
//    public Result findPage(@RequestParam("current") Integer current,
//                           @RequestParam("pageSize") Integer pageSize,
//                           @RequestParam("name") String name) {
//        IPage<Menu> page = new Page<>(current, pageSize);
//        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
//        menuQueryWrapper.eq("name", name);
//        menuQueryWrapper.orderByDesc("id");
//        return Result.success(menuService.page(page, menuQueryWrapper));
//    }

}

